package com.tyx.chargingpile.service.impl;

import com.tyx.chargingpile.Enum.ServiceMessageEume;
import com.tyx.chargingpile.vo.SocketResponseResult;
import com.tyx.chargingpile.service.ChargingPileService;
import com.tyx.chargingpile.service.MessageService;
import com.tyx.chargingpile.util.DateUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 电桩校时下行
 */
@Service("ChargingPileTimeDwn")
public class ChargingPileTimeDwn implements ChargingPileService {


    @Override
    public SocketResponseResult execute(ChannelHandlerContext channelHandlerContext, byte[] context) throws Exception {
        Map<String,String> dwn = new HashMap<>();
        dwn.put("time", DateUtils.format(DateUtils.now(),DateUtils.yyMMddHHmmss));
        byte[] dwnContext = MessageService.getMessage(dwn, ServiceMessageEume.PILETIME.getDwnLinePath());
        ByteBuf pingMessage = Unpooled.buffer();
        pingMessage.writeBytes(dwnContext);
        channelHandlerContext.writeAndFlush(pingMessage);
        return new SocketResponseResult(channelHandlerContext,dwnContext);
    }

    @Override
    public SocketResponseResult execute(ChannelHandlerContext channelHandlerContext, Map data) throws Exception {
        String time = (String) data.get("time");
        Map<String,String> dwn = new HashMap<>();
        dwn.put("time", time);
        byte[] dwnContext = MessageService.getMessage(dwn, ServiceMessageEume.PILETIME.getDwnLinePath());
        ByteBuf pingMessage = Unpooled.buffer();
        pingMessage.writeBytes(dwnContext);
        channelHandlerContext.writeAndFlush(pingMessage);
        return new SocketResponseResult(channelHandlerContext,dwnContext);
    }
}
